<?php 	
/**
 * 后台用户中心控制器
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Base;
use app\admin\model\admin;
use think\Session;
use think\Request;
class User extends Base{
	//判断是否是非法请求
	public function ispost(){
		if (!Request::instance()->isPost()){
			$this->redirect('index');
		}
	}
	public function index(){
		//php版本
		$this->assign('phpv', phpversion());
		//服务器版本
    	$this->assign('serverv', $_SERVER['SERVER_SOFTWARE']);
    	//最大上传数
    	$this->assign('updatamax', get_cfg_var ("upload_max_filesize"));
    	//服务器操作系统
    	$this->assign('osinfo', PHP_OS);
    	//获取网站统计信息
    	$this->assign('acco',db('blog')->count());
    	$this->assign('twco',db('twitter')->count());
    	$this->assign('caco',db('comment')->count());
    	$this->assign('lico',db('links')->count());
		return $this->fetch('admin/index');
	}
	public function userset(){
		//获取用户信息
		$user = new admin;
		$user = $user->find()->toArray();
		return $this->fetch('admin/userset',["user"=>$user]);
	}
	public function userexit(){
		session( null);
		$this->success('正在退出','./index/login/index');
	}
	public function useredit(){
		$this->ispost();
		//修改用户基本信息
		$user = admin::get(1);
		$user->nickname = input('nickname');
		$user->autograph = input('autograph');
		$user->email = input('email');
		$user->username = input('username');
		if ($user->save()) {
			//防止重新登录
			Session::set('username',input('username'));
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"修改失败"];
			echo json_encode($data);
		}
	}
	public function userpwd(){
		$this->ispost();
		//修改用户密码
		$oldpwd = input('oldpwd');
		$newpwd = input('newpwd');
		$user = admin::get(1);
		$info =$user->toArray();
		if (sha1($oldpwd.'(&8*5.)') == $info['password']) {
			$user->password = sha1($newpwd.'(&8*5.)');
			if ($user->save()) {
				Session::set('password',sha1($newpwd.'(&8*5.)'));
				$data = ["result"=>1, "msg"=>"修改成功"];
				echo json_encode($data);
			}else{
			$data = ["result"=>2, "msg"=>"修改失败！"];
			echo json_encode($data);
			}
		}else{
			$data = ["result"=>2, "msg"=>"修改失败，原密码错误！"];
			echo json_encode($data);
		}
	}
	//用户头像上传接口
	public function faceupload(){
		$file = request()->file('file');
		$info = $file->validate(['ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static/index/images','face');
		if($info){
			$data = [
				'code'=>0
			];
			echo json_encode($data);
		}else{
			$data = [
				'code'=>1,
			];
			echo json_encode($data);
		}
	}
}



 ?>